package com.yyfax.pay.order.data.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.util.CommonIdUtils;
import com.yyfax.pay.order.data.dao.PayinOrderDao;
import com.yyfax.pay.order.data.dao.PayinOrderLogDao;
import com.yyfax.pay.order.data.service.PayinOrderLogService;
import com.yyfax.pay.order.data.service.PayinOrderService;
import com.yyfax.pay.order.model.po.PayinOrderLogPO;
import com.yyfax.pay.order.model.po.PayinOrderPO;
import com.yyfax.pay.order.model.to.payin.PayinOrderVO;
import com.yyfax.pay.order.model.to.payin.PayinSnapshotVo;

/**
 * 代付订单服务实现
 *
 * @author maojian
 * @date 2017-04-26 11:09:15
 * @since 1.0.0
 */
@Service
public class PayinOrderServiceImpl extends BaseServiceImpl implements PayinOrderService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PayinOrderDao payinOrderDao;

	@Autowired
	private PayinOrderLogService payinOrderLogService;

	
	@Autowired
	private PayinOrderLogDao payinOrderLogDao;

	@Autowired
	private PayConstants payConstants;


	public PayinOrderServiceImpl(@Qualifier("payCacheService") CacheService cacheService){
		super(cacheService);
	}
	
	@Override
	public String saveAndReturnBusiKeyByBatch(PayinOrderPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空，返回-1");
			return null;
		}

		// 生成业务主键
		String busiId = CommonIdUtils.generateId(PayTypeEnum.BATCH_PAY_IN, po.getMerchantCode(), po.getOrderId());
		if (StringUtils.isBlank(po.getExpireTime())) { // 如果没有传订单过期时间，则默认生成过期时间
			long expireTime = DateUtil.str2DateTime(po.getOrderTime()).getTime() + payConstants.getDefaultExpireTime();
			po.setExpireTime(DateUtil.milli2str(expireTime));
		}

		po.setPayinOrderId(busiId);
		po.setCreateTime(DateUtil.getCurrentDateTime());
		payinOrderDao.insertReturnPK(po);

		return busiId;
	}
	
	@Override
	public String saveAndReturnBusiKey(PayinOrderPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空，返回-1");
			return null;
		}

		// 生成业务主键
		String busiId = CommonIdUtils.generateId(PayTypeEnum.BATCH_PAY_IN, po.getMerchantCode(), po.getOrderId());
		if (StringUtils.isBlank(po.getExpireTime())) { // 如果没有传订单过期时间，则默认生成过期时间
			long expireTime = DateUtil.str2DateTime(po.getOrderTime()).getTime() + payConstants.getDefaultExpireTime();
			po.setExpireTime(DateUtil.milli2str(expireTime));
		}

		po.setPayinOrderId(busiId);
		po.setCreateTime(DateUtil.getCurrentDateTime());
		payinOrderDao.insertReturnPK(po);

		return busiId;
	}

	
	@Override
	public PayinSnapshotVo querySnapshotByMerchantCodeAndOrderId(String merchantCode, String orderId) throws Exception {
		PayinSnapshotVo payinVo = new PayinSnapshotVo();
		PayinOrderPO payinOrderPO = queryByMerchantCodeAndOrderId(merchantCode, orderId);
		
		BeanUtils.copyProperties(payinOrderPO, payinVo);

		List<PayinOrderLogPO> payinLog = payinOrderLogDao.selectListByOrderId(merchantCode, orderId);
		payinVo.setOrderLogPOs(payinLog);

		return payinVo;
	}
	
	@Override
	public PayinOrderPO queryByMerchantCodeAndOrderId(String merchantCode, String orderId) throws Exception {
		if (StringUtils.isBlank(orderId) && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 orderId/merchantCode 为空 ");
			return null;
		}

		return payinOrderDao.selectByOrderIdAndMerchantCode(orderId, merchantCode);
	}

	@Override
	public List<PayinOrderVO> queryPayinOrderVoList(String merchantCode,
			String payinBatchOrderId) {
		if (StringUtils.isBlank(merchantCode) && StringUtils.isBlank(payinBatchOrderId)) {
			return null;
		}

		List<PayinOrderVO> payinOrderVOs = new ArrayList<PayinOrderVO>();

		List<PayinOrderPO> payinOrderPOs = payinOrderDao.selectByMerchantCodeAndPayinBatchOrderId(merchantCode, payinBatchOrderId);
		if (payinOrderPOs != null) {
			for (PayinOrderPO payinOrderPO : payinOrderPOs) {
				payinOrderVOs.add(trasformPayinOrderVO(payinOrderPO));
			}
		}

		return payinOrderVOs;
	}

	
	/**
	 * 持久化转换成快照类
	 * 
	 * @author maojian
	 * @date 2017-04-20 11:33:13
	 * @since v1.0.0
	 * @param payinOrderPO
	 * @return
	 */
	private PayinOrderVO trasformPayinOrderVO(PayinOrderPO payinOrderPO) {
		if (payinOrderPO == null) {
			return null;
		}

		PayinOrderVO payinOrderVO = new PayinOrderVO();
		BeanUtils.copyProperties(payinOrderPO, payinOrderVO);

		List<PayinOrderLogPO> payinOrderLogPOs = payinOrderLogService.queryListByPayinOrderId(payinOrderPO.getPayinOrderId(),
				payinOrderPO.getMerchantCode());
		payinOrderVO.setOrderLogPOs(payinOrderLogPOs);

		// 迭代判断订单关闭，和结果代码
		if (payinOrderLogPOs != null) {
			for (PayinOrderLogPO payinOrderLogPO : payinOrderLogPOs) {
				if (StringUtils.isBlank(payinOrderVO.getSerialNo()) && StringUtils.isNotBlank(payinOrderLogPO.getSerialNo())) { // 设置业务流水号
					payinOrderVO.setSerialNo(payinOrderLogPO.getSerialNo());
				}

			}
		}

		return payinOrderVO;
	}
}
